package org.netwia.networks.nodes;

import java.util.Map;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.netwia.networks.Node;
import org.netwia.utils.ValidationUtils;

/**
 * Implementation of node which passes input to the result.
 * If more than one input is received, then exception happen.
 * 
 * @author radek.hecl
 *
 * @param <T> type of the network parameter
 */
public class IdentityNode<T> implements Node<T> {

    /**
     * Builder object.
     * 
     * @param <T> type of the network parameter
     */
    public static class Builder<T> {

        /**
         * Builds the result object.
         * 
         * @return created object
         */
        public IdentityNode<T> build() {
            return new IdentityNode<T>(this);
        }
    }

    /**
     * Creates new instance.
     * 
     * @param builder builder object
     */
    public IdentityNode(Builder<T> builder) {
        guardInvariants();
    }

    /**
     * Guards this object to be consistent. Throws exception if this is not the case.
     */
    private void guardInvariants() {
    }

    @Override
    public T process(Map<String, T> inputs) {
        ValidationUtils.guardEquals(1, inputs.size(), "only 1 input is supported: inputs.keySet = " + inputs.keySet());
        for (String key : inputs.keySet()) {
            return inputs.get(key);
        }
        throw new RuntimeException("unknown situation, this is most likely bug in the source code");
    }
    
    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

}
